package ThreadPool.mvcc;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 事务实现类
 * @date 2022/5/23 14:33
 */
public class STMTxn implements Txn{
    //事务ID生成器
    private AtomicInteger atomicInteger = new AtomicInteger(0);

    //当前的事务所有的相关数据
    private Map<TxnRef, VersionedRef> inTxnMap = new HashMap<>();
    //当前事务所有需要修改的数据
    private Map<TxnRef, Object> writeMap = new HashMap<>();  //当前事务ID

    //当前事务ID
    private long txnId;

    STMTxn(){
        txnId = atomicInteger.incrementAndGet();
    }


    @Override
    public <T> T get(TxnRef<T> ref) {
       // 将读取到的数据加入inTxnMap
        if(!inTxnMap.containsKey(ref)){
            inTxnMap.put(ref,ref.ref);
        }
        return (T) inTxnMap.get(ref).value;
    }

    @Override
    public <T> void set(TxnRef<T> ref, T value) {
        // 将读取到的数据加入inTxnMap
        if(!inTxnMap.containsKey(ref)){
            inTxnMap.put(ref,ref.ref);
        }
        writeMap.put(ref,value);
    }

    //提交事务
    boolean commit(){
        /*synchronized (STM.commitLock){
            //是否校验通过
            boolean isValid = true;


        }*/
        return true;
    }


}
